package com.labichaoka.chaoka.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.util.SparseArray;

import com.labichaoka.chaoka.base.MyApplication;

/**
 * Created by haosunTsao on 2016/8/22.
 */
public class SharedPreferencesManager {


    public static class ADConfig {
        public static final String name = "ad_preference";
        public static final String KEY_AD_SERIALIZE_DATA = "ad_serialize_data";
        public static final String KEY_DISPLAY_AD = "ad_display";
        public static final String kEY_AD_LAST_DISPLAY_TIME = "ad_last_display_time";
        public static final String KEY_AD_DISPLAY_ENABLE = "ad_display_enable";
        public static final String KEY_AD_DISPLAY_TIMES = "ad_display_times";
    }

    /**
     * 与账户无关的数据
     */
    public static class ExtraConfig {
        public static final String name = "extra_preference";
    }

    public static final String A_DAY_DOWNLOAD_TIMES = "a_day_download_times";

    public static final String DAY_OF_YEAR = "day_of_year";

    public static final String REPAYMENT_COUNT = "repayment_count";

    public static final String KEY_SELECT_PERIOD = "key_select_period";

    public static final String KEY_SELECT_LOAN_MONEY = "key_select_loan_money";

    public static final String KEY_PUSH_ALIAS_STATUS = "KEY_PUSH_ALIAS_STATUS";

    public static final String KEY_PUSH_REGISTRATION_UPLOADED = "key_push_registration_uploaded";

    public static final String KEY_JDH_CREDIT_COUNT_DOWN_TIME = "key_jdh_credit_count_down_time";

    public static final String KEY_JDH_CREDIT_COUNT_DOWN_SYSTEM_TIME = "key_jdh_credit_count_down_system_time";

    public static final String KEY_JDH_LAST_CREDIT_STATE = "key_jdh_last_credit_state";

    public static final String KEY_HUO_TI_TYPE = "key_huoti_type";

    /**
     * 标记是否是第一次打开的key
     */
    public final String KEY_FIRST_START = "is_first_start";
    /**
     * 标记是否登录成功
     *
     */
    @Deprecated
    public static final String KEY_SUCCEED_LOGIN = "is_succeed_login";
    /**
     * token
     */
    public final static String TOKEN = "token";
    /**
     * 是否设置了登录密码
     */
    public final static String IS_SET_PWD = "is_set_pwd";
    /**
     * 是否设置了登录密码文案说明
     */
    public final static String IS_SET_PWD_MSG = "is_set_pwd_msg";
    /**
     * 短信登录显隐藏
     */
    public final static String MSG_LOGIN_STATUS = "msg_login_status";

    /**
     * 手机号
     */
    public final static String LOGIN_PHONE = "login_phone";

    public final static String UID = "user_id";

    //----------------------------------------------------------------------------------------------
    //商户ID
    public final String STORE_ID = "store_id";
    //商户描述
    public final String REMARK = "remark";
    //商户简称
    public final String ORG_SHORT_NAME = "org_short_name";
    //商户全称
    public final String ORG_NAME = "org_name";
    //商户所在地
    public final String ORG_ADDR = "org_addr";
    //商户所在地详细地址
    public final String ORG_ADDR_DETAILED = "org_addr_detailed";
    //业务类型
    public final String BUSINESS_TYPE = "business_type";
    //商户推荐码
    public final String RECO_CODE = "reco_code";

    //商品名称
    public final String LCOMMODITIES_ID = "lcommodities_id";
    //商品名称
    public final String LCOMMODITIES_CNAME = "lcommodities_cname";
    //商户头像
    public final static String LCOMMODITIES_ICON = "lcommodities_icon";

    //----------------------------------------------------------------------------------------------

    //场景消费可用额度，等于场景消费总额度-已经使用的场景消费额度
    public final String QUOTA_STATE = "quota_state";

    //场景消费可用额度，等于场景消费总额度-已经使用的场景消费额度
    public final String SCENE_CONS_AVAILABLE_AMOUNT = "scene_cons_available_amount";

    //场景消费总额度
    public final String SCENE_CONSTOTAL_AMOUNT = "scene_constotal_amount";

    //取现可用额度，等于取现总额度-已经使用的场景消费取现额度
    public final String TAKE_CASH_AVAILABLE_AMOUNT = "take_cash_available_amount";

    //取现总额度
    public final String TAKE_CASHTOTAL_AMOUNT = "take_cashtotal_amount";


    //额度计算中
    public final String QUOTA_STATE_ING = "quota_state_ing";


    private static final Context globalContext = MyApplication.mContext;

    /**
     * 存储的文件名
     */
    public static final String DATABASE = "Database";
    public static final String ORG_LOW_LOANMONEY = "LOW_LOAN_MONEY";
    private SharedPreferences sp;

    private static SparseArray<SharedPreferencesManager> sharedPreferencesUtilSparseArray = new SparseArray<>();


    public static SharedPreferencesManager getInstance(Context context) {
        return getInstance(context, null);
    }


    public static SharedPreferencesManager getInstance() {
        return getInstance(null, null);
    }

    public static SharedPreferencesManager getInstance(String name) {
        return getInstance(null, name);
    }

    public static synchronized SharedPreferencesManager getInstance(@Nullable Context context, @Nullable String name) {
        context = context == null ? globalContext : context.getApplicationContext();
        name = name == null ? DATABASE : name;
        int key = name.hashCode();
        SharedPreferencesManager instance = sharedPreferencesUtilSparseArray.get(key);
        if (instance == null) {
            instance = new SharedPreferencesManager(context, name);
            sharedPreferencesUtilSparseArray.put(key, instance);
        }
        return instance;
    }


    private SharedPreferencesManager(Context context, String name) {
        sp = context.getSharedPreferences(name, Activity.MODE_PRIVATE);
    }

    public void putString(String key, String value) {
        sp.edit().putString(key, value).commit();
    }

    public void putLong(String key, long value) {
        sp.edit().putLong(key, value).commit();
    }

    public String getString(String key, String defValue) {
        return sp.getString(key, defValue);
    }


    public long getLong(String key, long defaultValue) {
        return sp.getLong(key, defaultValue);
    }

    /**
     * SharedPreference key 的值变化的时候 回调listener
     *
     * @param listener
     */
    public void registerDataChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
        sp.registerOnSharedPreferenceChangeListener(listener);
    }

    public void clear() {
        sp.edit().clear().commit();
    }

    /**
     * 销毁监听器
     *
     * @param listener
     */
    public void unregisterDataChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
        sp.unregisterOnSharedPreferenceChangeListener(listener);
    }


    public void putBoolean(String key, Boolean value) {
        sp.edit().putBoolean(key, value).commit();
    }

    public boolean getBoolean(String key, Boolean defValue) {
        return sp.getBoolean(key, defValue);
    }


}
